function enable_carrier(carrier_ID,element)
{
    ajaxURL = AJAX_URL + '/enable_carrier/' + carrier_ID;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
			
				if(response == 1)
				{
					element.dialog("close");
					$('.carrier_button_span_' + carrier_ID).html(term_enabled);
					$(".carrier_no_" + carrier_ID).removeClass('ui-state-disabled')
					$(".carrier_button_" + carrier_ID).removeClass('ui-state-highlight');
					$("#" + carrier_ID).attr('rel','1');
				}
				else
				{
					alert(response);
				}
			
			}
			});
}
function disable_carrier(carrier_ID,element)
{
    ajaxURL = AJAX_URL + '/disable_carrier/' + carrier_ID;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
			
				if(response == 1)
				{
					element.dialog("close");
					$('.carrier_button_span_' + carrier_ID).html(term_disabled);
					$(".carrier_no_" + carrier_ID).addClass('ui-state-disabled')
					$(".carrier_button_" + carrier_ID).addClass('ui-state-highlight');
					$("#" + carrier_ID).attr('rel','0');
				}
				else
				{
					alert(response);
				}
			
			}
			});
}

function check_username()
{
    ajaxURL = AJAX_URL + '/check_username/';
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-carrier').serialize(),
			async: true,
			success: function(response){
												
                        reset_error_state('username_check');	
						
						var parsed = response.split('|');
                        
                        var html = '';
						
						html += parsed[1];

						$( "#username_status" ). css('font-weight','bold');						
						
						if(parsed[0] == "true")
						{
							$( "#username" ). removeClass('ui-state-error');
							$( "#username_status" ). css('color','green');
							
						}
						else
						{
							$( "#username" ). addClass('ui-state-error');
							$( "#username_status" ). css('color','red');	
						}
						
						$( "#username_status" ). html(parsed[1]);

						
			}
			});

}

function save_new_carrier()
{
    ajaxURL = AJAX_URL + '/add_new_carrier/';
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-carrier').serialize(),
			async: true,
			success: function(response){

                        reset_error_state('new_carrier');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						//alert(response);
						
                        if(parsed[0] == "true")
                        {
							reset_error_state('new_carrier');						
						
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                																					
						}
						else
						{
						   /*
							* ***********************************************************
							* SHOW RESPONSE 
							* ***********************************************************
							*/
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							
							
							
						   /*
							* ***********************************************************
							* MARK THE REQUIRED FIELDS
							* ***********************************************************
							*/
							fields = parsed[2].split('**');
						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).addClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).addClass('ui-state-error');
								}
							}
						}
			}
			});
}


function edit_carrier(carrier_ID)
{
    ajaxURL = AJAX_URL + '/edit_carrier/'+ carrier_ID;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-carrier').serialize(),
			async: true,
			success: function(response){

                        reset_error_state('new_carrier');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						//alert(response);
						
                        if(parsed[0] == "true")
                        {
							reset_error_state('new_carrier');						
						
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                																					
						}
						else
						{
						   /*
							* ***********************************************************
							* SHOW RESPONSE 
							* ***********************************************************
							*/
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							
							
							
						   /*
							* ***********************************************************
							* MARK THE REQUIRED FIELDS
							* ***********************************************************
							*/
							fields = parsed[2].split('**');
						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).addClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).addClass('ui-state-error');
								}
							}
						}
			}
			});
}

function save_settings()
{
        ajaxURL = AJAX_URL + '/save_settings/';
            
        //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#module-settings-form').serialize(),
			async: true,
			success: function(response){

                        reset_error_state('settings');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						if(parsed[0] == 'true')
						{
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                                    html += parsed[1] +"</p>";
                                
                                    $('#response')
                                        .addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                
						}
						else
						{
							if(parsed[1] == 'wrong_default_company_payment_delay')
							{
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                                    html += parsed[2] +"</p>";
							
                                    $('#default_company_payment_delay')
                                        .addClass('ui-state-error ui-corner-all');
										
									$('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                								
							}							
							else if(parsed[1] == 'wrong_default_carrier_payment_delay')
							{
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                                    html += parsed[2] +"</p>";
							
                                    $('#default_carrier_payment_delay')
                                        .addClass('ui-state-error ui-corner-all');
										
									$('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							}
						}
			}
			});


} 

function reset_error_state(form_name)
{

if(form_name == 'new_carrier')
{

	$('#response').removeClass('ui-state-error');
	
	var fields = new Array(
							"contract_type",
							"legal_name",
							"username",
							"password",
							"password_repeat",
							"rates_buy_emails",
							"rates_sell_emails",
							"sales_name",
							"sales_emails",
							"sales_phone",
							"billing_name",
							"billing_emails",
							"noc_name",
							"noc_emails",
							"noc_phone",
							"legal_emails",
							"company_phone",
							"time-zone-buy-select",
							"time-zone-sell-select",
							"currency",
							"address_line_1",
							"state",
							"city",
							"zip",
							"country",	
							"registration_id",
							"vat_id",
							"bank_name",
							"bank_address_1",
							"bank_city",
							"bank_state",
							"bank_country",
							"swift_code",
							"account_number",
							"account_holder",
							"company_balance",
							"company_credit_limit",
							"company_billing_period",
							"company_payment_delay",
							"carrier_balance",
							"carrier_credit_limit",
							"carrier_billing_period",
							"carrier_payment_delay"							
							);

						   /*
							* ***********************************************************
							* UNMARK ALL FIELDS
							* ***********************************************************
							*/						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).removeClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).removeClass('ui-state-error');
								}
							}
							
				
}
if(form_name == 'settings')
{
								$('#default_company_payment_delay')
                                        .removeClass('ui-state-error ui-corner-all');

								$('#default_carrier_payment_delay')
                                        .removeClass('ui-state-error ui-corner-all');									
										
								$('#response')
                                        .removeClass('ui-state-error ui-corner-all');
}
}

function change_contract_type()
{
	if($('#contract_type').val() == 'none')
	{
		// hide wrapper divs
		$('.left, .right, .bottom').hide();
	}
	else if($('#contract_type').val() == 'we_buy_from_them')
	{
		// show wrapper divs
		$('.left, .right, .bottom').show();
		
		$('#rates_buy_emails, .rates-buy-emails, #bank_information, #company_billing_information, .time-zone-buy').show();
		$('#rates_sell_emails, .rates-sell-emails, #carrier_billing_information, .time-zone-sell').hide();
	}
	else if($('#contract_type').val() == 'we_sell_to_them')
	{
		// show wrapper divs
		$('.left, .right, .bottom').show();
		
		$('#rates_buy_emails, .rates-buy-emails, #bank_information, #company_billing_information, .time-zone-buy').hide();		
		$('#rates_sell_emails, .rates-sell-emails, #carrier_billing_information, .time-zone-sell').show();
	}
	else if($('#contract_type').val() == 'bi_lateral')
	{
		// show wrapper divs
		$('.left, .right, .bottom').show();
		
		$('#rates_buy_emails, .rates-buy-emails, #bank_information, #company_billing_information, .time-zone-buy').show();		
		$('#rates_sell_emails, .rates-sell-emails, #carrier_billing_information, .time-zone-sell').show();		
	}
	
}